
********************************************************************************
********************************************************************************
*************************** THIS IS DO FILE 3 (SUF) ****************************
******* THIS FILE GENERATES THE CONDITIONAL DENIAL PROBABILITIES ***************
********************************************************************************
********************************************************************************

*Version: 2023 March 29


cap mkdir `"$output/log/relax_SUF_3_denial_probabilities"'
cap mkdir `"$output/temp"'

cap log close 
log using `"$output/log/relax_SUF_3_denial_probabilities/relax_SUF_3_denial_probabilities.smcl"', replace 

di "This run uses code version from 2023/03/29"

use `"$output/data/$line_file"', clear

*select sample first instance 
keep if R_1 == 1 & Y_0 == 0 			 // Keep if at least one resubmission 
assert Y_0 == 0 

gen d_1 = 1-Y_1
gen d_2 = 1-Y_2
gen d_3 = 1-Y_3
gen d_4 = 1-Y_4
gen d_5 = 1-Y_5

//compute amount percentiles
gegen bin_C = cut(line_item_value) , at(0(25)250)	
replace bin_C = 250 if bin_C == . 	// generate bins, bin claim amounts - width of bin = USD 25

********************************************************************************************
*** 1. Calculate denial probabilities for every procedure and save data to re-merge later***
********************************************************************************************

preserve 

	bys state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: gegen p_denial = mean(d_1)  	// Conditional probabilty that the first resubmission is paid
	
	keep state pay_type_pooled visit_code_cat p_denial bin_C proc1 dx1_code size_indicator
	
	bys state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: gen observations = _N

	bys state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: keep if _n == 1 
	
	gen resubmission = "1"
		
	save `"$output/temp/relax_SUF_denial_probabilities_bin_second_submission.dta"', replace 
	
restore 



*select sample second instance 
keep if R_2 == 1 & Y_1 == 0 			 // Keep if at least one resubmission 
assert Y_1 == 0 


********************************************************************************************
*** 1. Calculate denial probabilities for every procedure and save data to re-merge later***
********************************************************************************************

preserve 

	bys state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: gegen p_denial = mean(d_2) 	 	// Conditional probabilty that the second resubmission is paid
	
	keep state pay_type_pooled visit_code_cat p_denial bin_C proc1 dx1_code size_indicator
	
	bys state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: gen observations = _N

	bys state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: keep if _n == 1 
	
	gen resubmission = "2"

	save `"$output/temp/relax_SUF_denial_probabilities_bin_third_submission.dta"', replace 
	
restore 


*select sample second instance 
keep if R_3 == 1 & Y_2 == 0 			 // Keep if at least one resubmission 
assert Y_2 == 0 


********************************************************************************************
*** 1. Calculate denial probabilities for every procedure and save data to re-merge later***
********************************************************************************************

preserve 

	bys state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: gegen p_denial = mean(d_3) 		// Conditional probabilty that the third resubmission is paid
	
	keep state pay_type_pooled visit_code_cat p_denial bin_C proc1 dx1_code size_indicator
	
	bys state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: gen observations = _N

	bys state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: keep if _n == 1 
	
	gen resubmission = "3"
	
	save `"$output/temp/relax_SUF_denial_probabilities_bin_fourth_submission.dta"', replace 
	
restore 



*select sample second instance 
keep if R_4 == 1 & Y_3 == 0 			 // Keep if at least one resubmission 
assert Y_3 == 0 


********************************************************************************************
*** 1. Calculate denial probabilities for every procedure and save data to re-merge later***
********************************************************************************************

preserve 

	bys state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: gegen p_denial = mean(d_4)  	// Conditional probabilty that the fourth resubmission is paid
	
	keep state pay_type_pooled visit_code_cat p_denial bin_C proc1 dx1_code size_indicator
	
	bys state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: gen observations = _N

	bys state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: keep if _n == 1 
	
	gen resubmission = "4"
	
	save `"$output/temp/relax_SUF_denial_probabilities_bin_fifth_submission.dta"', replace 
	
restore 


*select sample second instance 
keep if R_4 == 1 & Y_3 == 0 			 // Keep if at least one resubmission 
assert Y_3 == 0 


********************************************************************************************
*** 1. Calculate denial probabilities for every procedure and save data to re-merge later***
********************************************************************************************

preserve 

	bys state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: gegen p_denial = mean(d_5) 			// Conditional probabilty that the fifth resubmission is paid
	
	keep state pay_type_pooled visit_code_cat p_denial bin_C proc1 dx1_code size_indicator
	
	bys state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: gen observations = _N

	bys state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: keep if _n == 1 
	
	gen resubmission = "5"
	
	save `"$output/temp/relax_SUF_denial_probabilities_bin_sixth_submission.dta"', replace 
	
restore 



********Generate means

** Bin 
use `"$output/temp/relax_SUF_denial_probabilities_bin_second_submission.dta"', clear
append using `"$output/temp/relax_SUF_denial_probabilities_bin_third_submission.dta"'
append using `"$output/temp/relax_SUF_denial_probabilities_bin_fourth_submission.dta"'
append using `"$output/temp/relax_SUF_denial_probabilities_bin_fifth_submission.dta"'
append using `"$output/temp/relax_SUF_denial_probabilities_bin_sixth_submission.dta"'

bys state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: gegen mean_p_denial = mean(p_denial) [aweight=observations]

bys state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: gegen total_observations = sum(observations)

sort state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator resubmission
by state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: gen resubmissions_used_str = resubmission[1] + resubmission[2] + resubmission[3] + resubmission[4] + resubmission[5]
bys state pay_type_pooled visit_code_cat bin_C proc1 dx1_code size_indicator: keep if _n == 1 

gen resubmissions_used = strlen(resubmissions_used_str) 
keep state pay_type_pooled visit_code_cat mean_p_denial total_observations resubmissions_used bin_C proc1 dx1_code size_indicator
rename mean_p_denial p_denial
save `"$output/temp/relax_SUF_mean_denial_probabilities_bin.dta"', replace 

erase `"$output/temp/relax_SUF_denial_probabilities_bin_second_submission.dta"'
erase `"$output/temp/relax_SUF_denial_probabilities_bin_third_submission.dta"'
erase `"$output/temp/relax_SUF_denial_probabilities_bin_fourth_submission.dta"'
erase `"$output/temp/relax_SUF_denial_probabilities_bin_fifth_submission.dta"'
erase `"$output/temp/relax_SUF_denial_probabilities_bin_sixth_submission.dta"'

log close 


